use Cwd;
use strict;

#Checkout the Test_Report from the branch
#system("svn checkout http://cad-test/repo/crdm/device/firmware/tools/build_tool/branches/Test_Report");
mkdir("Test_Report");
mkdir("Test_Report/Test_Cases");
#Get date and time and then print them
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
my $month = ("January","February","March","April", "May", "June","July", "August",
		"September", "October",  "November", "December")[$mon];
my $timestamp = sprintf "$month\-%02d,%04d-%02d:%02d", $mday, $year + 1900,$hour, $min;
#Write the running time in the Test_Report file
open(FILE,">>Test_Report/Test_Cases/$timestamp.txt") or die "Couldn't create file";
print FILE "RUN TIME: $timestamp\n";
close FILE;

#Run each test suite and write the output in the Test_Report file
print 'Running tests...';
print "\nRunning Test Suite 1";
runTests("TS1.pl");
print "\nRunning Test Suite 2";
runTests("TS2.pl");
print "\nRunning Test Suite 3";
runTests("TS3.pl");
print "\nRunning Test Suite 4";
runTests("TS4.pl");
print "\nRunning Test Suite 5";
runTests("TS5.pl");
print "\nRunning Test Suite 6";
runTests("TS6.pl");
print "\nRunning Test Suite 7";
runTests("TS7.pl");
print "\nFinished \n";
updateTCReport("TEST SUITE 1","ts1_result.txt","ts1_errors.txt");
updateTCReport("TEST SUITE 2","ts2_result.txt","ts2_errors.txt");
updateTCReport("TEST SUITE 3","ts3_result.txt","ts3_errors.txt");
updateTCReport("TEST SUITE 4","ts4_result.txt","ts4_errors.txt");
updateTCReport("TEST SUITE 5","ts5_result.txt","ts5_errors.txt");
updateTCReport("TEST SUITE 6","ts6_result.txt","ts6_errors.txt");
updateTCReport("TEST SUITE 7","ts7_result.txt","ts7_errors.txt");
tagTCReport();
system("rm -rf Test_Report/Test_Cases/ts1_errors.txt");
system("rm -rf Test_Report/Test_Cases/ts1_result.txt");
system("rm -rf Test_Report/Test_Cases/ts2_errors.txt");
system("rm -rf Test_Report/Test_Cases/ts2_result.txt");
system("rm -rf Test_Report/Test_Cases/ts3_errors.txt");
system("rm -rf Test_Report/Test_Cases/ts3_result.txt");
system("rm -rf Test_Report/Test_Cases/ts4_errors.txt");
system("rm -rf Test_Report/Test_Cases/ts4_result.txt");
system("rm -rf Test_Report/Test_Cases/ts5_errors.txt");
system("rm -rf Test_Report/Test_Cases/ts5_result.txt");
system("rm -rf Test_Report/Test_Cases/ts6_errors.txt");
system("rm -rf Test_Report/Test_Cases/ts6_result.txt");
system("rm -rf Test_Report/Test_Cases/ts7_errors.txt");
system("rm -rf Test_Report/Test_Cases/ts7_result.txt");

sub updateTCReport{
	#Old file
	my $test_case_rel = "<Test_Report/Test_Cases/$_[1]";
	my $test_case_err = "<Test_Report/Test_Cases/$_[2]";
	#Updated file
	my $report = ">>Test_Report/Test_Cases/$timestamp.txt";
	open(TC_R, $test_case_rel);
	open(TC_E, $test_case_err);	
	open(REPORT, $report);
	my @lines;
	print REPORT "\n\n" ,$_[0] ,"\n";
	print REPORT "\n ERRORS: \n\n";
	print REPORT @lines;
	#Copy the rest of the lines from old file
	@lines = <TC_E>;
	print REPORT @lines;
	@lines = <TC_R>;
	print REPORT "\n RESULT: \n\n";
	print REPORT @lines;
	close REPORT;
	close TC_E;
	close TC_R;	
}

sub tagTCReport{

	#system("svn add Test_Report/Test_Cases");	
	#system("svn commit Test_Cases -m \"Adding test report Windows illegal name exception\"");
	#system("svn copy http://cad-test/repo/crdm/device/firmware/tools/build_tool/branches/Test_Report/Test_Cases http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/Test%20-%20reports/ -m \"adding Test Report Windows illegal name exception\"");

	#Delete the Test_Report directory
	#system("rm -rf Test_Report");
}


#Subroutine to run each test suite and write the output into the test report
sub runTests{
	my $test = $_[0];
	system("perl $test");
}
